﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NeoseekerDesktop.Properties;
using System.Xml;

namespace NeoseekerDesktop.Forms
{
    public partial class SettingsForm : Form
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public SettingsForm()
        {
            InitializeComponent();

            // Insert Default Values
            this.UsernameTextBox.Text = Settings.Default.Username;
            this.TokenTextBox.Text = Settings.Default.Token;
        }

        /// <summary>
        /// Event handler for when the "Test Connection" button is pressed.  Tests connection to Neoseeker API and validation.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TestConnectionButton_Click(object sender, EventArgs e)
        {
            try
            {
                XmlTextReader xmlReader = new XmlTextReader(MainForm.constuctAPICallURL(MainForm.PMInfoAPI, this.UsernameTextBox.Text, this.TokenTextBox.Text));
                while (xmlReader.Read())
                {
                    // Do Nothing
                }
                MessageBox.Show("Test successful.");
            }
            catch (System.Net.WebException)
            {
                MessageBox.Show("API Call Failed!");
            }
            catch (XmlException)
            {
                MessageBox.Show("Username / token combo is invalid!");
            }

        }

        /// <summary>
        /// Event handler for when the "Save" button is pressed.  Saves the settings.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SaveSettingsButton_Click(object sender, EventArgs e)
        {
            Settings.Default.Username = this.UsernameTextBox.Text;
            Settings.Default.Token = this.TokenTextBox.Text;
            Settings.Default.Save();
            this.Close();
        }

        /// <summary>
        /// Event handler for when the "Cancel" button is pressed.  Disregards settings and closes form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelSettingsButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
